<?php 
class news extends Controller {

var $event 				= '';
var	$obj_news			= '';
var $m_nid				= '';
var $m_language			= 'eng';
/* Paging*/
var $ctr_name			= '';
var	$ncurrent_page		= 1;
var	$nrow_per_page		= 5;
var $ntotal_row			= 0;
/* Paging*/
var $nid_section		= '';
var $nid_cat			= '';
var $img_cpt			= '';

var	$title					= '';
var	$tags					= '';
var	$description			= ''; 

var $txt_name			= '';
var	$txt_title			= '';
var $txt_content		= '';

var $tag_detail			= '';
var $title_detail		= '';
var $short_detail		= '';

function news()
	{
		parent::Controller();	
		session_start();
		
		$this->load->database();	
		$this->load->helper('ap_function');
		$this->load->helper('ap_object');
		$this->load->helper('ap_html');
		$this->load->helper('ap_view_helper');
		$this->load->helper('ap_db');
		$this->load->helper('ap_module');
		$this->load->language('ap', 'eng');
		
		//$this->load->model('fr_news_model');
	}

// Dinh nghia ham rut gon khi set language cho cac label.
// 
private function m_language_key($str_key)
{
	return $this->lang->line('lbl.news.'.$str_key);
}

function detail($nid)
{
	
	//update hit for article
	$data_news = get_news_byid_detail($nid);
	$arr_data = array(								
				'nhit'				=> $data_news['nhit']+1
		        );
	$this->db->where('nid', $nid);
	$this->db->update(Fget_ap_table('tnews'), $arr_data);

	$this->m_nid = $nid;
	$this->event = 'detail';
	$this->do_process();
}

function page($current_page)
{	
	$this->ncurrent_page			= $current_page;
	$this->do_process();
}

function index()
{
	$this->do_process();
}

function cat($ccode)
	{
		$data['data_news']	= get_page_news($ccode);
		$this->load->view('page_news',$data);
	}

function do_process() 
{
	$this->get_data(); 		
	$this->caculate_data(); 		
	$this->do_business(); 		
	$this->destroy_data();
}
	
private function get_data()
{  
	if(isset($_POST['txt_name']))
	{
		$this->txt_name = $_POST['txt_name'];
		$this->txt_title = $_POST['txt_title'];
		$this->txt_content = $_POST['txt_content'];
		$arr_data =	array(						
			'nid_news'			=> $this->m_nid,	
			'status'			=>	1,
			'cname'				=> $this->txt_name,
			'ctitle'			=> $this->txt_title,
			'ccomment'			=> $this->txt_content,
			'ddate01'			=> dbget_current_date()
		);
		$this->db->insert(Fget_ap_table('tcomment'), $arr_data);	
		redirect ('news/detail/'.$this->m_nid);	
	//	$this->db->update(Fget_ap_table('tcomment'), $arr_data);
	}	
}
	
private function caculate_data()
{
	$this->load->language('ap', $this->m_language);
	$str_where_news			= $this->get_where_news_string();
	//$this->ntotal_row		= $this->fr_news_model->count_record($this->m_nid_cat_news);
	$this->ctr_name			= $this->uri->segment(1);
	//$this->obj_news			= $this->fr_news_model->get_listview($str_where_news,'ddate01','asc',$this->nrow_per_page,$this->ncurrent_page,$this->ntotal_row);
	if($this->event == 'detail')
	{
		$this->obj_news			= get_news_byid_detail($this->m_nid);
		$this->nid_section		= $this->obj_news['nid_section_news'];
		$this->nid_cat			= $this->obj_news['nid_cat_news'];
		$this->title		 	= $this->obj_news['ctitle'];
		$this->description	 	= strip_tags($this->obj_news['cshort_content']);
		$this->tags		 		= $this->obj_news['ctag'];
	}
 	$this->event			= $this->event == ''?'view':$this->event;
	
}

private function do_business()
{	
	
	$data['obj_news']	= $this->obj_news;
	$data['lbl_tinmoi']	= $this->m_language_key('tinmoi');
	$data['lbl_tin']	= $this->m_language_key('tin');
	$data['link_page']	= base_url().'index.php/news';
	$data['event']		= $this->event;
	$data['m_nid']		= $this->m_nid;
	$data['nid_cat']	= $this->nid_cat;
	$data['nid_sec']	= $this->nid_section;
	
	$data['menu_sec']		= '';
	$data['menu_cat']		= '';
	$data['g_ishome']		= 0;
	
	if($this->m_nid == 345)
		$data['menu_top']			= 'about_us';
	else
		$data['menu_top']			= '';
	
	$data['img_cpt'] 			= $this->img_cpt;
	if(isset($this->img_cpt['word']))
	{
		$data['hidden_cpt'] 	= $this->img_cpt['word'];
	}
	else
		$data['hidden_cpt'] 	= '';
	
	
	if($this->event == 'detail')
	{
		$data['title']			= $this->title;
		$data['tags']			= $this->tags;
		$data['description']	= $this->description;
		
		$data['menu_cat']			= $this->nid_cat;
		$data['menu_news']			= $this->m_nid;
		
		$this->load->view('news_detail/index',$data);	
	}
	else
	{
		$data['title']			= $this->title;
		$data['tags']			= $this->tags;
		$data['description']	= $this->description;
		
		$data['menu_cat']		= '';
		$data['menu_news']		= '';
		
		$this->load->view('news/index',$data);	
	}
}
	
private function destroy_data()
{
			
}
private function get_where_news_string()
{
//		$str		= 'where nstatus = "1"';	
		//$str	   = 'where ccode_cat_news = "'.$this->m_nid_cat_news.'" ';
		//if($this->m_nid !='')
		//	$str	   .= 'AND nid = "'.$this->m_nid.'" ';
//Dieu kien get record nstatus
//0:Khong active
//1:Active

		//return $str ;
}

}